package com.dx.financehy.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dx.financehy.base.PageDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 */
@ApiModel(value = "PageUtils", description = "分页工具类")
public class PageUtils implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "总记录数")
    private int totalCount;

    @ApiModelProperty(value = "每页记录数")
    private int pageSize;

    @ApiModelProperty(value = "总页数")
    private int totalPage;

    @ApiModelProperty(value = "当前页数")
    private int currPage;

    @ApiModelProperty(value = "列表数据")
    private List<?> list;

    /**
     * 分页
     *
     * @param list    列表数据
     * @param pageDTO 请求参数
     */

    public PageUtils(List<?> list, PageDTO pageDTO) {
        this.list = PageUtil.toPage(pageDTO.getPage() - 1, pageDTO.getSize(), list);
        this.totalCount = list.size();
        this.pageSize = pageDTO.getSize();
        this.currPage = pageDTO.getPage();
        this.totalPage = (int) Math.ceil((double) pageDTO.getSize() / pageDTO.getPage());
    }

    public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
    }

    /**
     * 分页
     */
    public PageUtils(IPage<?> page) {
        this.list = page.getRecords();
        this.totalCount = (int) page.getTotal();
        this.pageSize = (int) page.getSize();
        this.currPage = (int) page.getCurrent();
        this.totalPage = (int) page.getPages();
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

}
